<?php
namespace BP\DA;
require_once("java/io/FileInputStream.php");
require_once("java/io/IOException.php");
require_once("java/io/InputStream.php");
require_once("java/text/DecimalFormat.php");
require_once("java/text/SimpleDateFormat.php");
require_once("java/util/Date.php");
require_once("org/apache/poi/hssf/usermodel/HSSFCell.php");
require_once("org/apache/poi/hssf/usermodel/HSSFDateUtil.php");
require_once("org/apache/poi/hssf/usermodel/HSSFRow.php");
require_once("org/apache/poi/hssf/usermodel/HSSFSheet.php");
require_once("org/apache/poi/hssf/usermodel/HSSFWorkbook.php");
class DBLoad {
	public static function ImportTableInto ($impTb, $intoTb, $select, $clear) // [DataTable impTb, String intoTb, String select, int clear]
	{
		$count = 0;
		return $count;
	}
	public static function GenerFirstTableName ($fileName) // [String fileName]
	{
		return DBLoad::GenerTableNameByIndex($fileName, 0);
	}
	public static function GenerTableNameByIndex ($fileName, $index) // [String fileName, int index]
	{
		$excelSheets = DBLoad::GenerTableNames($fileName);
		if ((($excelSheets != NULL) && (count($excelSheets) /*from: excelSheets.length*/ >= $index)))
		{
			return $excelSheets[$index];
		}
		return NULL;
	}
	public static function GenerTableNames ($fileName) // [String fileName]
	{
		return NULL;
	}
	protected static function getValue ($hssfCell) // [HSSFCell hssfCell]
	{
		if (($hssfCell->getCellType() == $hssfCell->CELL_TYPE_BOOLEAN))
		{
			return $String->valueOf($hssfCell->getBooleanCellValue());
		}
		if (($hssfCell->getCellType() == $hssfCell->CELL_TYPE_NUMERIC))
		{
			$style = $hssfCell->getCellStyle()->getDataFormat();
			if ($HSSFDateUtil->isCellDateFormatted($hssfCell))
			{
				$date = $hssfCell->getDateCellValue();
				switch ($style) {
					case 178:
						return new SimpleDateFormat("yyyy'�?'M'�?'d'�?'")->format($date);
					case 14:
						return new SimpleDateFormat("yyyy/MM/dd")->format($date);
					case 179:
						return new SimpleDateFormat("yyyy/MM/dd HH:mm")->format($date);
					case 181:
						return new SimpleDateFormat("yyyy/MM/dd HH:mm a ")->format($date);
					case 22:
						return new SimpleDateFormat(" yyyy/MM/dd HH:mm:ss ")->format($date);
					default:
						break;
				}
			}
			else
			{
				switch ($style) {
					case 9:
						return new DecimalFormat("0.00%")->format($hssfCell->getNumericCellValue());
					case 57:
						return new SimpleDateFormat(" yyyy'�?'MM'�?' ")->format($hssfCell->getDateCellValue());
					default:
						$hssfCell->setCellType($HSSFCell->CELL_TYPE_STRING);
						return $String->valueOf($hssfCell->getStringCellValue());
				}
			}
		}
		$hssfCell->setCellType($HSSFCell->CELL_TYPE_STRING);
		return $String->valueOf($hssfCell->getStringCellValue());
	}
	public static function GetTableByExt_InputStream ($is) // [InputStream is]
	{
		$Tb = DataTable::constructor__String("Tb");
		$Tb->Rows->clear();
		$collection = DataColumnCollection::constructor__DataTable($Tb);
		$hssfWorkbook = NULL;
		try 
		{
			$hssfWorkbook = new HSSFWorkbook($is);
		}
		catch (\Exception $e)
		{
			$e->getMessage(); // 
		}
		$hssfSheet = $hssfWorkbook->getSheetAt(0);
		if (($hssfSheet == NULL))
		{
			return NULL;
		}
		$row_size = $hssfSheet->getPhysicalNumberOfRows();
		for ($j = 0; ($j < $row_size); ++$j) 
		{
			$hssfRow = $hssfSheet->getRow($j);
			if (($hssfRow == NULL))
			{
				continue;
			}
			$call_num = $hssfRow->getPhysicalNumberOfCells();
			if ((0 == $j))
			{
				for ($k = 0; ($k < $call_num); ++$k) 
				{
					$hssfCell = $hssfRow->getCell($k);
					if ((NULL == $hssfCell))
					{
						continue;
					}
					$column = DataColumn::constructor__String(DBLoad::getValue($hssfCell));
					$collection->Add_DataColumn($column);
				}
			}
			else
			{
				$dataRow = DataRow::constructor__DataTable($Tb);
				for ($k = 0; ($k < $call_num); ++$k) 
				{
					$hssfCell = $hssfRow->getCell($k);
					if ((NULL == $hssfCell))
					{
						continue;
					}
					$dataRow->setValue($collection->get($k), DBLoad::getValue($hssfCell));
				}
				$Tb->Rows->add($dataRow);
			}
		}
		$Tb->Columns = $collection;
		return $Tb;
	}
	public static function GetTableByExt_String ($filePath) // [String filePath]
	{
		$stream = NULL;
		$dataTable = NULL;
		try 
		{
			$stream = new FileInputStream($filePath);
			$dataTable = DBLoad->GetTableByExt($stream);
		}
		catch (\Exception $e)
		{
			$e->getMessage(); // 
		}
		finally {
		{
			if (($stream != NULL))
			{
				try 
				{
					$stream->close();
				}
				catch (\Exception $e)
				{
					$e->getMessage(); // 
				}
			}
		}
		}
		return $dataTable;
	}
}
?>
